from datetime import datetime

from apps.aiauth.models import Users
from utils import restful, md5
from .models import *
from .serializers import ExpertsSerializers, UserInfoSerializer


# 新增时段
def addTimesLot(request):
    Timeslot.objects.create(
        name=request.POST.get('name'),
        create_time=datetime.now(),
        status=1,
        timeslot_count=0,
        can_del=1,
    )
    return restful.ok(message='添加成功！')


# 修改时段
def updateTimesLot(request):
    timeslot_id = request.POST.get('timeslot_id')
    name = request.POST.get('name')
    obj = Timeslot.objects.get(timeslot_id=timeslot_id)
    obj.name = name
    obj.update_time = datetime.now()
    obj.save()
    return restful.ok(message='修改成功！')


# 删除时段
def deleteTimesLot(request):
    timeslot_id = request.POST.get('timeslot_id')
    try:
        obj = Timeslot.objects.get(timeslot_id=timeslot_id, can_del=True)
        obj.delete()
        return restful.ok(message='删除成功！')
    except:
        return restful.unauth(message="无法删除该时段！")


# 新增专家
def addExpert(request):
    Experts.objects.create(
        experts_name=request.POST.get('name'),
        create_time=datetime.now(),
        status=1,
        careerplan_count=0,
        can_del=1
    )
    return restful.ok(message="添加成功！")


# 删除专家
def deleteExpert(request):
    experts_id = request.POST.get('experts_id')
    try:
        obj = Experts.objects.get(experts_id=experts_id, can_del=True)
        obj.delete()
        return restful.ok(message="删除成功！")
    except:
        return restful.unauth(message="无法删除该专家！")


# 更新用户密码
def updatePass(request):
    user_id = request.POST.get('user_Id')
    rpassWord = request.POST.get('rpassWord')  # 获取旧密码，进行判断
    passWord = request.POST.get('passWord')  # 新密码
    try:
        user = Users.objects.get(user_id=user_id, password=md5.md5(rpassWord))
        user.password = md5.md5(passWord)
        user.save()
        return restful.ok(message="密码修改成功！")
    except:
        return restful.params_error(message="旧密码错误，请重新输入！")


# 更新用户信息
def updateUserInfo(request):
    user_id = request.POST.get('user_Id')
    userinfo_Id = request.POST.get('userinfo_Id')
    email = request.POST.get('email')
    phoneNumber = request.POST.get('phoneNumber')
    qqId = request.POST.get('qqId')
    weChatId = request.POST.get('weChatId')
    sex = request.POST.get('sex')
    if sex == 'true':
        sex = True
    else:
        sex = False
    politicalVisage = request.POST.get('politicalVisage')
    placeOfOrigin = request.POST.get('placeOfOrigin')
    nation = request.POST.get('nation')
    age = request.POST.get('age')
    user = Users.objects.get(user_id=user_id)

    user.email = email
    user.phonenumber = phoneNumber
    user.qqid = qqId
    user.wechatid = weChatId
    user.save()
    userinfo = Userinfo.objects.get(userinfo_id=userinfo_Id)
    userinfo.sex = sex
    userinfo.politicalvisage = politicalVisage
    userinfo.placeoforigin = placeOfOrigin
    userinfo.nation = nation
    if age:
        userinfo.age = age
    userinfo.save()
    data = UserInfoSerializer(userinfo).data

    return restful.ok('修改成功！', data=data)


# 根据userid获取专家信息
def getExpert(request):
    user_id = request.GET.get('user_id')
    try:
        expert = Experts.objects.get(user_id=user_id)
        data = ExpertsSerializers(expert).data
        return restful.ok(data=data)
    except:
        return restful.ok(message="没有获取到id为{}的数据".format(user_id))
